package com.v2future.sjt.collegecheckingattendance.fruit;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.v2future.sjt.collegecheckingattendance.LazyFragment;
import com.v2future.sjt.collegecheckingattendance.R;
import com.v2future.sjt.collegecheckingattendance.common.activity.DatePickerActivity;
import com.v2future.sjt.collegecheckingattendance.common.activity.MipcaActivityCapture;
import com.v2future.sjt.collegecheckingattendance.fruit.adapter.FruitAdapter;
import com.v2future.sjt.collegecheckingattendance.fruit.adapter.FruitConditionTypeAdapter;
import com.v2future.sjt.collegecheckingattendance.fruit.bean.ConditionBean;
import com.v2future.sjt.collegecheckingattendance.fruit.bean.ConditionResponeBean;
import com.v2future.sjt.collegecheckingattendance.fruit.bean.ConditionTypeBean;
import com.v2future.sjt.collegecheckingattendance.fruit.bean.FruitBean;
import com.v2future.sjt.collegecheckingattendance.fruit.research.ResearchTypeListActivity;
import com.v2future.sjt.collegecheckingattendance.home.bean.LoginResultBean;
import com.v2future.sjt.collegecheckingattendance.utils.BaseResponseBean;
import com.v2future.sjt.collegecheckingattendance.utils.Constants;
import com.v2future.sjt.collegecheckingattendance.utils.IntentKey;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.BaseRequestParams;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.NetTaskListener;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.NetUtils;
import com.v2future.sjt.collegecheckingattendance.widget.ListViewInScroll;
import com.v2future.sjt.collegecheckingattendance.widget.view.PullToRefreshView;

import org.w3c.dom.Text;
import org.xutils.x;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import me.nereo.multi_image_selector.bean.Image;

/**
 * 成果界面
 * @author shijianting
 * @time 2016/3/26 14:01
 */
public class FruitFragment extends LazyFragment {

    PullToRefreshView pullView;
    ListView fruitLv;
    List<ConditionBean> dataList;
    FruitAdapter adapter;
    FruitConditionTypeAdapter fruitConditionTypeAdapter;

    ConditionTypeResponseBean conditionTypeResponseBean;
    ConditionTypeBean selectConditionTypeBean;//选中的类型

    /**
     * 轨迹类型列表
     */
    ListView conditionTypeLv;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_fruit, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        conditionTypeLv = (ListView) view.findViewById(R.id.lv_condition_type);
        conditionTypeLv.setOnItemClickListener(itemClickListener);
    }

    @Override
    protected void lazyLoad() {
        getBaseActivity().showTitleLoading();
        doGetConditionType();
    }

    /**
     * 轨迹类型刷新
     */
    private void refreshConditionType(){
        fruitConditionTypeAdapter = new FruitConditionTypeAdapter(conditionTypeResponseBean.getData(), getBaseActivity());
        conditionTypeLv.setAdapter(fruitConditionTypeAdapter);
    }

    /**
     * 获取轨迹类型
     */
    public void doGetConditionType(){
        BaseRequestParams params = new BaseRequestParams(getBaseActivity(), getBaseActivity().formatUrl(Constants.GET_CONDITION_TYPE));
        NetUtils.doGetRequest(getBaseActivity(), params, new NetTaskListener<ConditionTypeResponseBean>() {

            /**
             * 数据获取完成
             *
             * @param result
             */
            @Override
            public void onSuccess(ConditionTypeResponseBean result) {
                conditionTypeResponseBean = result;
                refreshConditionType();
            }

            /**
             * 位置网络错误
             */
            @Override
            public void onError() {

            }

            /**
             * 非正常状态的数据
             *
             * @param result
             */
            @Override
            public void onInterError(ConditionTypeResponseBean result) {
                getBaseActivity().tip(result.getFailReason());
            }

            /**
             * 网络错误
             *
             * @param responseCode
             * @param responseMsg
             * @param errorResult
             */
            @Override
            public void onNetError(int responseCode, String responseMsg, String errorResult) {

            }

            /**
             * 请求取消
             */
            @Override
            public void onCancelled() {

            }

            /**
             * 网络请求流程结束
             */
            @Override
            public void onFinished() {
                getBaseActivity().cancelTitleLoading();
                getBaseActivity().cancelLoadingDialog();
            }

            /**
             * 缓存获取完成
             *
             * @param result
             */
            @Override
            public void onSuccessCache(ConditionTypeResponseBean result) {

            }
        });
    }

    /**
     * 轨迹网络回复实体
     */
    public class ConditionTypeResponseBean extends BaseResponseBean implements Serializable {

        private List<ConditionTypeBean> data;

        public List<ConditionTypeBean> getData() {
            return data;
        }

        public void setData(List<ConditionTypeBean> data) {
            this.data = data;
        }
    }

    /**
     * 轨迹类型
     */
    private AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectConditionTypeBean = conditionTypeResponseBean.getData().get(position);
            Intent intent;
            if ("科研".equals(selectConditionTypeBean.getConditonTypeName()) && "6".equals(selectConditionTypeBean.getConditonTypeId())) {
                intent = new Intent(getBaseActivity(), ResearchTypeListActivity.class);
            }
            else {
                intent = new Intent(getBaseActivity(), FruitOneTypeActivity.class);
            }

            intent.putExtra(IntentKey.FRUIT_TYPE, selectConditionTypeBean);
            startActivity(intent);
        }
    };

}
